/*
 * \bried  Nitpicker view implementation
 * \author Norman Feske
 * \date   2006-08-09
 */

/*
 * Copyright (C) 2006-2009 Norman Feske
 * Genode Labs, Feske & Helmuth Systementwicklung GbR
 *
 * This file is part of the Genode OS framework, which is distributed
 * under the terms of the GNU General Public License version 2.
 */

#include <base/printf.h>

#include "view.h"
#include "clip_guard.h"
#include "session.h"
#include "draw_label.h"


/***************
 ** Utilities **
 ***************/

/**
 * Draw rectangle
 */
static void draw_rect(Canvas *canvas, int x, int y, int w, int h, Color color)
{
	canvas->draw_box(Rect(Point(x, y),         Area(w, 1)), color);
	canvas->draw_box(Rect(Point(x, y),         Area(1, h)), color);
	canvas->draw_box(Rect(Point(x + w - 1, y), Area(1, h)), color);
	canvas->draw_box(Rect(Point(x, y + h - 1), Area(w, 1)), color);
}


/**
 * Draw outlined frame with black outline color
 */
static void draw_frame(Canvas *canvas, Rect r, Color color)
{
	/* draw frame around the view */
	draw_rect(canvas, r.x1() - 3, r.y1() - 3, r.w() + 6, r.h() + 6, BLACK);
	draw_rect(canvas, r.x1() - 2, r.y1() - 2, r.w() + 4, r.h() + 4, color);
	draw_rect(canvas, r.x1() - 1, r.y1() - 1, r.w() + 2, r.h() + 2, BLACK);
}


/**********
 ** View **
 **********/

void View::title(const char *title)
{
	Nitpicker::strncpy(_title, title, TITLE_LEN);

	/* calculate label size, the position is defined by the view stack */
	_label_rect = Rect(Point(0, 0), label_size(_session->label(), _title));
}


void View::frame(Canvas *canvas, Mode *mode)
{
	/* do not draw frame in flat mode */
	if (mode->flat()) return;

	draw_frame(canvas, *this,
	           (mode->focused_view() == this) ? WHITE : FRAME_COLOR);
}


void View::draw(Canvas *canvas, Mode *mode)
{
	/* use dimming in x-ray mode */
	Canvas::Mode op = mode->flat() ? Canvas::SOLID : Canvas::MIXED;

	/* is this the currently focused view? */
	bool focused_view = (mode->focused_view() == this);
	Color frame_color = focused_view ? WHITE : FRAME_COLOR;

	/* do not dim the focused view in x-ray mode */
	if (mode->xray() && !mode->kill() && focused_view)
		op = Canvas::SOLID;

	/*
	 * The view content and label should never overdraw the
	 * frame of the view in non-flat Nitpicker modes. The frame
	 * is located outside the view area. By shrinking the
	 * clipping area to the view area, we protect the frame.
	 */
	Clip_guard clip_guard(canvas, *this);

	/*
	 * If the clipping area shrinked to zero,
	 * we do not process drawing operations.
	 */
	if (!canvas->clip_valid() || !_session) return;

	/* draw view content */
	Color mix_color = mode->kill() ? KILL_COLOR : BLACK;
	canvas->draw_texture(_session->texture(), mix_color, _buffer_off + p1(), op);

	if (mode->flat()) return;

	/* draw label */
	draw_label(canvas, _label_rect.p1(), _session->label(), WHITE, _title, frame_color);
}
